/**
 * Copyright 2008 floop.pl
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.sqlgen.builders;

import java.util.Iterator;

import org.eaicompiler.model.LocalizedText;
import org.eaicompiler.model.ScriptDefinition;

/**
 * Builder for localized texts SQL
 * 
 * @author floop.pl
 */
public class LocalizedTextsSQLBuilder extends SQLBuilder {
	private ScriptDefinition	script;
	private StringBuilder		sql;
	private boolean				sqlBuilt	= false;
	private int					creatureId;
	private boolean				useDelete;

	public LocalizedTextsSQLBuilder(ScriptDefinition script, boolean useDelete, int creatureId) {
		super();
		this.useDelete = useDelete;
		this.script = script;
		this.creatureId = creatureId;
		this.sql = new StringBuilder();
	}

	/**
	 * @param useDelete
	 *            the useDelete to set
	 */
	public void setUseDelete(boolean useDelete) {
		this.useDelete = useDelete;
	}

	public String buildSQLForCreature() {
		if (!sqlBuilt) {
			// Generate proper deletion script
			if (useDelete) {
				if (script.getLocalizedText().size() > 0) {
					sql.append("delete from `localized_texts` where `id` in (");
					Iterator<LocalizedText> it = script.getLocalizedText().iterator();
					while (it.hasNext()) {
						LocalizedText t = it.next();
						sql.append("'");
						sql.append(creatureId * 100 + t.getId() - 1);
						sql.append("'");
						if (it.hasNext()) {
							sql.append(",");
						}
					}
					sql.append(");\n");
				}
			}
			if (script.getLocalizedText().size() > 0) {
				sql.append("insert into `localized_texts` (`id`, `locale_0`, `locale_2`, `locale_3`) values\n");
				Iterator<LocalizedText> it = script.getLocalizedText().iterator();
				while (it.hasNext()) {
					LocalizedText t = it.next();
					sql.append("\t('");
					sql.append(creatureId * 100 + t.getId() - 1);
					sql.append("',");
					sql.append("'");
					sql.append(escapeString(t.getLocalizedText(LocalizedText.ENGLISH)));
					sql.append("',");
					if (t.getLocalizedText(LocalizedText.FRENCH) != null) {
						sql.append("'");
						sql.append(escapeString(t.getLocalizedText(LocalizedText.FRENCH)));
						sql.append("',");
					} else {
						sql.append("'',");
					}
					if (t.getLocalizedText(LocalizedText.GERMAN) != null) {
						sql.append("'");
						sql.append(escapeString(t.getLocalizedText(LocalizedText.GERMAN)));
						sql.append("'");
					} else {
						sql.append("''");
					}
					sql.append(")");
					if (it.hasNext()) {
						sql.append(",\n");
					}
				}
				sql.append(";\n");
			}
			sqlBuilt = true;
		}
		return sql.toString();
	}
	
}
